package com.hghivln.controller.erp;

import com.hghivln.aspectj.annotation.Log;
import com.hghivln.aspectj.enums.BusinessType;
import com.hghivln.controller.BaseController;
import com.hghivln.pojo.dto.ProviderDto;
import com.hghivln.pojo.vo.AjaxResult;
import com.hghivln.service.ProviderService;
import com.hghivln.utils.ShiroUtils;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;

@RestController
@RequestMapping("erp/provider")
public class ProviderController extends BaseController {

    @Reference
    private ProviderService providerService;


    /**
     * 分页查询
     */
    @HystrixCommand
    @GetMapping("listProviderForPage")
    public AjaxResult listProviderForPage(ProviderDto providerDto) {
        return providerService.listProviderForPage(providerDto);
    }


    /**
     * 添加
     */
    @HystrixCommand
    @PostMapping("addProvider")
    @Log(title = "添加供应商",businessType = BusinessType.INSERT)
    public AjaxResult addProvider(@Validated ProviderDto providerDto) {
        providerDto.setSimpleUser(ShiroUtils.getCurrentSimpleUser());
        return providerService.addProvider(providerDto);
    }

    /**
     * 修改
     */
    @HystrixCommand
    @PutMapping("updateProvider")
    @Log(title = "修改供应商",businessType = BusinessType.UPDATE)
    public AjaxResult updateProvider(@Validated ProviderDto providerDto) {
        providerDto.setSimpleUser(ShiroUtils.getCurrentSimpleUser());
        return providerService.updateProvider(providerDto);
    }

    /**
     * 删除
     */
    @HystrixCommand
    @DeleteMapping("deleteProviderByIds/{ids}")
    @Log(title = "删除供应商",businessType = BusinessType.DELETE)
    public AjaxResult deleteProviderByIds(@PathVariable @Validated @NotEmpty(message = "要删除的供应商ID不能为空")Long[] ids) {
        return providerService.deleteProviderByIds(ids);
    }

    /**
     * 根据ID查询一个供应商信息
     */
    @HystrixCommand
    @GetMapping("getProviderById/{id}")
    public AjaxResult getProviderById(@PathVariable @Validated @NotEmpty(message = "供应商ID不能为空") Long id) {
        return providerService.selectProviderById(id);
    }

    /**
     * 查询所有有效的供应商信息
     */
    @HystrixCommand
    @GetMapping("selectAllProvider")
    public AjaxResult selectAllProvider() {
        return this.providerService.selectAllProvider();
    }
}
